package com.kalix.btzjjj.txt.biz;

import com.kalix.btzjjj.memory.api.model.HttpRequestUtil;
import com.kalix.btzjjj.txt.api.biz.IServerManagerBeanService;
import com.kalix.btzjjj.txt.api.dao.IServerManagerBeanDao;
import com.kalix.btzjjj.txt.api.model.InfUtil;
import com.kalix.btzjjj.txt.entities.ServerManagerBean;
import com.kalix.framework.core.impl.biz.GenericBizServiceImpl;
import com.kalix.framework.core.util.ConfigUtil;

/**
 *@ Author  ZhaoHang
 * @Date  2019/8/28
 **/
public class ServerManagerBeanServiceImpl extends GenericBizServiceImpl<IServerManagerBeanDao, ServerManagerBean> implements IServerManagerBeanService {
    String txtIp = (String) ConfigUtil.getConfigProp("FREEBOARD_TXT_IP","config.btzjjj.portconf");
    @Override
    public String getValue() {
        String str = InfUtil.interfaceUtil(txtIp,"");
//        String str = HttpRequestUtil.getXpath(txtIp);
        String key = str.substring(0, 304);
        String all = str.substring(304, str.length());
//        JSONArray jArray = ReadFileUtil.ReadFileUtil();
//        System.out.println(jArray.toString());
        String last = "";
        //拆分字符串按照第一次出现数字
//        Pattern pattern = Pattern.compile("[0-9]");
//        Matcher matcher = pattern.matcher(ss);
//        if (matcher.find()) {
//            System.out.println(ss.indexOf(matcher.group()));
//            String first = ss.substring(0, ss.indexOf(matcher.group()));
//            String second = ss.substring(ss.indexOf(matcher.group()), ss.length());
//            System.out.println("first:----------"+first);
//            System.out.println("second:----------"+second);
//        }
//        if (jArray.length() > 1) {
        String[] name = key.split(",");
        String[] value = all.split(",");
        String longestUptimeOS = null;
        String shortestUptimeOS = null;
        String OSAlarmPercent =null;
        String serverCount = null;
        String VMCount = null;
        for (int i = 0; i < name.length; i++) {
            if ("longestUptimeOS".equals(name[i])) {
                    longestUptimeOS=value[i];
            }
            if ("shortestUptimeOS".equals(name[i])) {
                    shortestUptimeOS=value[i];
            }
            if ("OSAlarmPercent".equals(name[i])) {
                    OSAlarmPercent=value[i];
            }
            if ("serverCount".equals(name[i])) {
                serverCount = value[i];
            }
            if ("VMCount".equals(name[i])) {
                VMCount = value[i];
            }
        }
        String[] longest =null;
        int longestCount =0;
        String longestDay ="";
        String longestHour ="";
        String longestMinute ="";
        String longestCountValue="";
        String longestCountName="";
        if(longestUptimeOS.equals("-")){
            longest[0]= "-";
            longestCountValue="-";
            longestDay = "-";
            longestHour = "-";
            longestMinute = "-";
        }else {
//            String[] longest=longestUptimeOS.replace("|",",").split(",");
            longest = longestUptimeOS.split("\\|");
            longestCount = Integer.parseInt(longest[2]);
            longestDay = String.valueOf(longestCount / 86400);
            longestHour = String.valueOf(longestCount % 86400 / 3600);
            longestMinute = String.valueOf(longestCount % 86400 % 3600 / 60);
//                longestCountValue=String.valueOf(longestCount/(60*60*24));
            if((longestCount/60)<60){
                longestCountValue=String.valueOf(longestCount/60);
                longestCountName="分钟";
            }else if((longestCount/60)>60&&(longestCount/60)<1440){
                longestCountValue=String.valueOf(longestCount/3600);
                longestCountName="小时";
            }else {
                longestCountValue=String.valueOf(longestCount/86400);
                longestCountName="天";
            }
        }
        int shortestCount =0;
        String shortestDay ="";
        String shortestHour ="";
        String[] shortest =null;
        String shortestMinute ="";
        String shortestCountValue="";
        String shortestCountName="";
        if(shortestUptimeOS.equals("-")){
            shortest [0]="-" ;
            shortestDay = "-";
            shortestHour = "-";
            shortestMinute = "-";
            shortestCountValue="-";
        }else {
            shortest = shortestUptimeOS.split("\\|");
            shortestCount = Integer.parseInt(shortest[2]);
            shortestDay = String.valueOf(shortestCount / 86400);
            shortestHour = String.valueOf(shortestCount % 86400 / 3600);
            shortestMinute = String.valueOf(shortestCount % 86400 % 3600 / 60);
//                shortestCountValue=String.valueOf(shortestCount/(60*60));
            if((shortestCount/60)<60){
                shortestCountValue=String.valueOf(shortestCount/60);
                shortestCountName="分钟";
            }else if((longestCount/60)>60&&(shortestCount/60)<1440){
                shortestCountValue=String.valueOf(shortestCount/3600);
                shortestCountName="小时";
            }else {
                shortestCountValue=String.valueOf(shortestCount/86400);
                shortestCountName="天";
            }
        }
        String  serverAlarm="";
        String  VMAlarm="";
        if(OSAlarmPercent.equals("-")){
            serverAlarm="-";
            VMAlarm="-";
        }else{
            serverAlarm=String.valueOf((int)(Double.parseDouble(OSAlarmPercent.replace("%","")))*(Integer.parseInt(serverCount)+Integer.parseInt(VMCount))/100);
            VMAlarm=String.valueOf((Double.parseDouble(OSAlarmPercent.replace("%",""))));
        }
        last = "{" +
                "\"bar\":{" +
                "\"name_1\":\""+longest[0]+"\"," +
                "\"day_1\":"+longestDay+"," +
                "\"hour_1\":\""+longestHour+"\"," +
                "\"minute_1\":\""+longestMinute+"\"," +
                "\"y_name1\":\""+longestCountName+"\"," +
                "\"name_2\":\""+shortest[0]+"\"," +
                "\"day_2\":\""+shortestDay+"\"," +
                "\"hour_2\":\""+shortestHour+"\"," +
                "\"minute_2\":\""+shortestMinute+"\"," +
                "\"y_name2\":\""+shortestCountName+"\"," +
                "\"yData\":[\""+shortest[0]+"\",\""+longest[0]+"\"]," +
                "\"data\":[\""+shortestCountValue+"\",\""+longestCountValue+"\"]" +
                "}," +
                "\"pie\": [\""+serverCount+"\"," +
                "\""+serverAlarm+"\"," +
                "\""+VMCount+"\"," +
                "\""+VMAlarm+"\"]" +
                "}";
//        } else {
//            last = jArray.toString();
//        }
        return last;
    }
}
